package com.tbit.uqbike.service.base.impl;

import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.object.pojo.TerControlMsg;
import com.tbit.uqbike.service.base.MQProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("mQProducerService")
@Slf4j
public class MQProducerImpl implements MQProducer {
    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public void sendDataToQueue(String queueKey, String msg) {
        try {
            log.info(msg);
            amqpTemplate.send(queueKey, new Message(msg.getBytes("UTF-8"), new MessageProperties()));
        } catch (Exception e) {
            log.error("连接RabbitMQ异常", e);
        }
    }

    @Override
    public void sendDataToQueue(String queueKey, String feedback, Integer msgId, Object data) {
        TerControlMsg terControlMsg = new TerControlMsg();
        terControlMsg.setFeedback(feedback);
        terControlMsg.setMsgId(msgId);
        terControlMsg.setData(data);
        sendDataToQueue(queueKey, JSON.toJSONString(terControlMsg));
    }

}  